package andy.ham;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Crazy on 2018/6/19.
 */

public class AssignActivity extends AppCompatActivity{
    String username,password;
    EditText name;
    EditText psw;
    TextView regist;
    TextView visitor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assign);

        //返回登录标题
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("登录");

        name = (EditText)findViewById(R.id.Regist_username);
        psw = (EditText)findViewById(R.id.Regist_password);
        regist = (TextView)findViewById(R.id.Regist_regist);

        //点击注册按钮
        regist.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View view) {
                username = name.getText().toString();
                password = psw.getText().toString();
                //判断输入是否为空
                if(username.isEmpty()||password.isEmpty()){
                    Toast.makeText(AssignActivity.this, "账户密码皆不能为空", Toast.LENGTH_SHORT).show();
                }
                else {
                    //若不为空，保存信息，传递信息，跳转页面
                    user user=new user();
                    user.setUsername(username);
                    user.setPassword(password);
                    user.save();
                    Toast.makeText(AssignActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(AssignActivity.this,LoginActivity.class);
                    AssignActivity.this.finish();
                }
            }
        });

        //点游客登录按钮:
        visitor = (TextView)findViewById(R.id.Regist_visitor);
        visitor.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(AssignActivity.this, "Welcome!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(AssignActivity.this,MyDiary.class);
                startActivity(intent);
                AssignActivity.this.finish();
            }
        });

    }

    //使注册界面返回登录界面
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent();
        intent.setClass(AssignActivity.this,LoginActivity.class);
        startActivity(intent);
        AssignActivity.this.finish();
        System.out.println("返回");
    }

}
